<!DOCTYPE HTML>
<html>
<head>
    <!-- support for mobile touch devices -->
    <meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1, maximum-scale=1">

    <!-- twitter bootstrap CSS stylesheet - not required by cornerstoneTools -->
    <link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">

    <link href="../dialogPolyfill.css" rel="stylesheet">

    <link href="../cornerstone.min.css" rel="stylesheet">

    <style>
        /* prevents selection when left click dragging */
        .disable-selection {
            -moz-user-select: none; -webkit-user-select: none; -ms-user-select:none; user-select:none;
        }
        /* prevents cursor from changing to the i bar on the overlays*/
        .noIbar {
            cursor:default;
        }
        .annotationDialog, .relabelDialog {
            z-index: 1000;
            position: absolute;
            margin: 0px;
            left: 40%;
            top: 40%;
            width: 300px;
            border: 1px black solid;
            border-radius: 5px;
        }
        .annotationTextInputOptions {
            padding: 5px 0px;
        }
        .annotationTextInput {
            margin-left: 5px;
        }
        .annotationDialogConfirm {
            float: right;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="page-header">
        <h1>
            Annotation Example
        </h1>
        <p>
            This page contains an example of the text annotation tool
        </p>
        <a href="../index.html">Go back to the Examples page</a>
    </div>

    <div class="row">
        <div class="col-xs-12 col-sm-2">
            <ul class="list-group">
                <a id="enable" class="list-group-item">Enable</a>
                <a id="activate" class="list-group-item">Activate</a>
                <a id="deactivate" class="list-group-item">Deactivate</a>
                <a id="disable" class="list-group-item">Disable</a>
            </ul>
        </div>
        <div class="col-xs-6">
            <div style="width:256px;height:256px;position:relative;display:inline-block;color:white;"
                 oncontextmenu="return false"
                 class='cornerstone-enabled-image disable-selection noIbar'
                 unselectable='on'
                 onselectstart='return false;'
                 onmousedown='return false;'>
                <div id="dicomImage"
                     style="width:256px;height:256px;top:0px;left:0px; position:absolute;">
                </div>
            </div>
            <dialog class="annotationDialog">
                <h5>Enter your annotation</h5>
                <div class="annotationTextInputOptions">
                    <label for="annotationTextInput">New label</label>
                    <input name="annotationTextInput" class="annotationTextInput" type="text"/>
                </div>
                <a class="annotationDialogConfirm btn btn-sm btn-primary">OK</a>
            </dialog>
            <dialog class="relabelDialog" oncontextmenu="return false">
                <h5>Edit your annotation</h5>
                <div class="annotationTextInputOptions">
                    <label for="annotationTextInput">New label</label>
                    <input name="annotationTextInput" class="annotationTextInput" type="text"/>
                </div>
                <div>
                    <a class="relabelRemove btn btn-sm btn-secondary">Remove marker</a>
                    <a class="relabelConfirm btn btn-sm btn-primary">OK</a>
                </div>
            </dialog>
        </div>

    </div>

</body>

<!-- include the hammer.js library for touch gestures-->
<script src="../hammer.min.js"></script>

<!-- include the cornerstone library -->
<script src="../cornerstone.min.js"></script>
<script src="../cornerstoneMath.min.js"></script>

<!-- include the cornerstone tools library -->
<script src="../../dist/cornerstoneTools.js"></script>
<script>window.cornerstoneTools || document.write('<script src="https://unpkg.com/cornerstone-tools">\x3C/script>')</script>

<!-- include special code for these examples which provides images -->
<script src="../exampleImageLoader.js"></script>

<script src="../dialogPolyfill.js"></script>

<script>
    // Register the dialogs using the HTML5 Dialog Polyfill
    var annotationDialog = document.querySelector('.annotationDialog');
    var relabelDialog = document.querySelector('.relabelDialog');
    dialogPolyfill.registerDialog(annotationDialog);
    dialogPolyfill.registerDialog(relabelDialog);

    var element = document.getElementById('dicomImage');

    var imageId = 'example://1';

    // Define a callback to get your text annotation
    // This could be used, e.g. to open a modal
    function getTextCallback(doneChangingTextCallback) {
        var annotationDialog  = document.querySelector('.annotationDialog');
        var getTextInput = annotationDialog.querySelector('.annotationTextInput');
        var confirm = annotationDialog.querySelector('.annotationDialogConfirm');

        annotationDialog.showModal();

        confirm.removeEventListener('click', closeHandler);
        confirm.addEventListener('click', closeHandler);

        annotationDialog.removeEventListener("keydown", keyPressHandler);
        annotationDialog.addEventListener('keydown', keyPressHandler);

        function keyPressHandler(e) {
            // If Enter is pressed, close the dialog
            if (e.which === 13) {
                closeHandler();
            }
        }

        function closeHandler() {
            annotationDialog.close();
            doneChangingTextCallback(getTextInput.value);
            // Reset the text value
            getTextInput.value = "";
        }
    }

    // Define a callback to edit your text annotation
    // This could be used, e.g. to open a modal
    function changeTextCallback(data, eventData, doneChangingTextCallback) {
        var relabelDialog = document.querySelector('.relabelDialog');
        var getTextInput = relabelDialog.querySelector('.annotationTextInput');
        var confirm = relabelDialog.querySelector('.relabelConfirm');
        var remove = relabelDialog.querySelector('.relabelRemove');

        getTextInput.value = data.text;
        relabelDialog.showModal();

        function confirmHandler() {
           relabelDialog.close();
           doneChangingTextCallback(data, getTextInput.value);
        }

        confirm.removeEventListener('click', confirmHandler);
        confirm.addEventListener('click', confirmHandler);

        // If the remove button is clicked, delete this marker
        function removeHandler() {
          relabelDialog.close();
          doneChangingTextCallback(data, undefined, true);
        }

        remove.removeEventListener('click', removeHandler);
        remove.addEventListener('click', removeHandler);

        function keyPressHandler(e) {
            // If Enter is pressed, close the dialog
            if (e.which === 13) {
                closeHandler();
            }
        }

        relabelDialog.removeEventListener("keydown", keyPressHandler);
        relabelDialog.addEventListener('keydown', keyPressHandler);

        function closeHandler() {
            relabelDialog.close();
            doneChangingTextCallback(data, getTextInput.value);
            // Reset the text value
            getTextInput.value = "";
        }
    }

    var config = {
        getTextCallback : getTextCallback,
        changeTextCallback : changeTextCallback,
        drawHandles : false,
        drawHandlesOnHover : true,
        arrowFirst : true
    }

    // image enable the dicomImage element
    cornerstone.enable(element);
    cornerstoneTools.mouseInput.enable(element);
    cornerstoneTools.touchInput.enable(element);

    cornerstone.loadImage(imageId).then(function(image) {
        cornerstone.displayImage(element, image);

        // Try commenting this out to see the default behaviour
        // By default, the tool uses Javascript's Prompt function
        // to ask the user for the annotation. This example uses a
        // slightly nicer HTML5 dialog element.
        cornerstoneTools.arrowAnnotate.setConfiguration(config);

        // Enable all tools we want to use with this element
        cornerstoneTools.arrowAnnotate.activate(element, 1);
        cornerstoneTools.arrowAnnotateTouch.activate(element);

        activate("activate");

        function activate(id) {
            document.querySelectorAll('a').forEach(function(elem) {              elem.classList.remove('active');            });
            document.getElementById(id).classList.add('active');
        }

        // Tool button event handlers that set the new active tool
        document.getElementById('enable').addEventListener('click', function() {
            activate("enable");
            cornerstoneTools.arrowAnnotate.enable(element);
            cornerstoneTools.arrowAnnotateTouch.enable(element);
            return false;
        });
        document.getElementById('disable').addEventListener('click', function() {
            activate("disable");
            cornerstoneTools.arrowAnnotate.disable(element);
            cornerstoneTools.arrowAnnotateTouch.disable(element);
            return false;
        });
        document.getElementById('activate').addEventListener('click', function() {
            activate("activate");
            cornerstoneTools.arrowAnnotate.activate(element, 1);
            cornerstoneTools.arrowAnnotateTouch.activate(element);
            return false;
        });
        document.getElementById('deactivate').addEventListener('click', function() {
            activate("deactivate");
            cornerstoneTools.arrowAnnotate.deactivate(element, 1);
            cornerstoneTools.arrowAnnotateTouch.deactivate(element);
            return false;
        });
    });

</script>
</html>
